package com.ts.api.module.api.service.impl;

import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.entity.bo.ApiDataExchangeBO;
import com.ts.api.module.api.entity.bo.ApiParameterBO;
import com.ts.api.module.api.entity.bo.ApiPhoneMaskDataExchangeBO;
import com.ts.api.module.api.entity.vo.ReqApiParameterVO;
import com.ts.api.module.api.handler.ApiDataExchangeHandler;
import com.ts.api.module.api.service.ApiBizDataExchangeService;
import com.ts.api.module.match.plan.PlanDataExchangeFacade;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.match.plan.internal.qualification.model.PlanQualificationError;
import com.ts.api.module.match.plan.internal.qualification.model.PlanQualificationResult;
import com.ts.api.module.match.plan.model.ApiPlanMatch;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ApiBizDataExchangeServiceImpl implements ApiBizDataExchangeService {

    @Autowired
    private ApiDataExchangeHandler apiDataExchangeHandler;
    @Autowired
    private PlanDataExchangeFacade planDataExchangeFacade;

    public ApiDataExchangeBO dataExchange(ApiParameterBO apiParameterBO) throws Exception {
        //  获取参数
        ReqApiParameterVO reqApiParameterVO = apiParameterBO.getReqApiParameterVO();
        ApiUserRequest apiUserRequest = reqApiParameterVO.getApiUserRequest();
        String phoneMd5 = apiUserRequest.getPhoneMd5();

        //  md5撞库限流-如果出现并发时将此次流程放弃，抛出异常
        apiDataExchangeHandler.handleLimitMd5(phoneMd5);
        //  重复用户处理
        apiDataExchangeHandler.handleLocalExist(apiParameterBO);

        //  搜索可分发的计划列表
        ApiPlanMatch apiPlanMatch = ApiPlanMatch.ofMatch(apiParameterBO);
        apiPlanMatch.enableAllowMultiPush();
        PlanQualificationResult planQualificationResult = planDataExchangeFacade.searchDistributePlan(apiPlanMatch);

        //  抛出错误信息
        PlanQualificationError planQualificationError = planQualificationResult.getValidateFail();
        if (ValidParam.isNotNull(planQualificationError)) {
            throw planQualificationError.getExp();
        }

        //  撞库流程
        PlanDistributeResult planDistributeResult = planDataExchangeFacade.apiDataExchange(planQualificationResult);
        Assert.isTrueThrows(planDistributeResult.getDistributeStatus(), new ApiMsgException(ApiEnum.API_DATA_EXCHANGE_MATCH_FAIL3));

        //  获取最终匹配结果数据
        ProductPlanPO finalMatchPlanPO = planDistributeResult.getProductPlanPO();

        //  检测匹配流程一致性，无冲突之前的流程则插入流程日志
        apiDataExchangeHandler.checkMatchConsistency(apiParameterBO, finalMatchPlanPO, phoneMd5);

        //  返回业务实体
        return ApiDataExchangeBO.of(planDistributeResult);
    }

    @Override
    public ApiPhoneMaskDataExchangeBO dataExchangePhoneMask(ApiParameterBO apiParameterBO, Integer maskLen) throws Exception {
        //  获取参数
        ReqApiParameterVO reqApiParameterVO = apiParameterBO.getReqApiParameterVO();
        ApiUserRequest apiUserRequest = reqApiParameterVO.getApiUserRequest();

        //  md5撞库限流-如果出现并发时将此次流程放弃，抛出异常
        String phoneMask = apiUserRequest.getPhoneMask();
        String phoneMaskSub = phoneMask.substring(0, maskLen);
        String maskMd5 = MD5.create().digestToHex(phoneMaskSub);
        apiDataExchangeHandler.handleLimitMd5(maskMd5);

        //  搜索可分发的计划列表
        ApiPlanMatch apiPlanMatch = ApiPlanMatch.ofMatch(apiParameterBO);
        apiPlanMatch.disableAllowMultiPush();
        PlanQualificationResult planQualificationResult = planDataExchangeFacade.searchDistributePlan(apiPlanMatch);

        //  返回错误信息
        PlanQualificationError planQualificationError = planQualificationResult.getValidateFail();
        if (ValidParam.isNotNull(planQualificationError)) {
            throw planQualificationError.getExp();
        }

        //  撞库流程
        List<PlanDistributeResult> listPlanDistributePhoneMaskResult = planDataExchangeFacade.apiPhoneMaskDataExchange(planQualificationResult);
        if (ValidParam.isEmpty(listPlanDistributePhoneMaskResult)) {
            throw new ApiMsgException(ApiEnum.API_DATA_EXCHANGE_MATCH_FAIL3);
        }

        //  直接返回插入
        apiDataExchangeHandler.checkPhoneMaskMatchConsistency(apiParameterBO, phoneMask);

        //  合并返回字段-返回业务实体
        String reqId = planQualificationResult.getReqId();
        return ApiPhoneMaskDataExchangeBO.of(reqId, listPlanDistributePhoneMaskResult);
    }
}
